package com.back.controller;

import com.back.common.utils.Result;
import com.back.common.utils.SecurityUtil;
import com.back.entity.CompanyVerificationLog;
import com.back.service.ICompanyVerificationLogService;
import com.back.vo.CompanyVerificationLogVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  企业认证记录前端控制器
 * </p>
 *
 * @author liyang
 * @since 2025-04-26
 */
@RestController
@RequestMapping("/company/verification-log")
public class CompanyVerificationLogController {

    @Autowired
    private ICompanyVerificationLogService verificationLogService;

    /**
     * 获取企业认证记录列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasRole('ADMIN')")
    public Result getVerificationLogList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String status) {
        Page<CompanyVerificationLog> page = new Page<>(current, size);
        IPage<CompanyVerificationLogVO> logList = verificationLogService.getVerificationLogList(page, status);
        return Result.ok().data("logList", logList);
    }

    /**
     * 获取认证记录详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasRole('ADMIN')")
    public Result getVerificationLogDetail(@PathVariable Long id) {
        CompanyVerificationLogVO log = verificationLogService.getVerificationLogDetail(id);
        return Result.ok().data("log", log);
    }

    /**
     * 审核企业认证
     */
    @PostMapping("/{id}/review")
    @PreAuthorize("hasRole('ADMIN')")
    public Result reviewVerification(
            @PathVariable Long id,
            @RequestParam String status,
            @RequestParam(required = false) String reason) {
        Long adminId = SecurityUtil.getCurrentUserId();
        CompanyVerificationLogVO log = verificationLogService.reviewVerification(id, adminId, status, reason);
        return Result.ok().data("log", log);
    }
}
